﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.Data.Items;
using Workflow.Alerts.DAL;
using Workflow.Alerts.Interfaces.ProfileSettings;
using System.Data;
using workflow.Dal;

namespace Workflow.Alerts
{
    public class ArchiveTimePresenter : Presenter
    {
        #region members
        private readonly IArchiveTimeView view;
        private readonly UserDAL model;
        #endregion

        #region constructor
        public ArchiveTimePresenter(IArchiveTimeView _view)
            : this(_view, null)
        { }

        public ArchiveTimePresenter(IArchiveTimeView _view, ISessionProvider session)
            : base(_view, session)
        {
            view = base.GetView<IArchiveTimeView>();

            view.OnPageLoad += view_Load;
            view.OnUpdate += view_Update;
            view.OnExport += view_Export;
            model = new UserDAL();
        }

        #endregion

        #region private methods

        private void view_Update()
        {
            var filterSql = GetFilterSql();
            model.UpdateArchiveTime(filterSql);
        }

        private void view_Load()
        {
            var filterSql = GetFilterSql();
            view.Days = model.GetArchiveTime(filterSql);
        }

        private WorkflowFilterSql GetFilterSql()
        {
            var filter = new WorkflowFilter
            {
                MunicipalityId = CurrentUser.MunicipalityId,
                Days = view.Days
            };

            var filterSql = new WorkflowFilterSql(filter);
            return filterSql;
        }

        private void view_Export()
        {
            var filterSql = GetFilterSql();            
            var archive = model.GetArchiveExportGrid(filterSql);
            view.ArchiveExportGrid = AddColumns(archive);
        }

        private DataTable AddColumns(DataTable archive)
        {
            archive.Columns.Add("activitytypename");
            archive.Columns.Add("currentstatusname");
            foreach (DataRow row in archive.Rows)
            {
                row["activitytypename"] = WorkflowEnums.ActivityTypeList[(int)row["activitytype"]];
                row["currentstatusname"] = ((Workflow.Alerts.WorkflowEnums.Status)row["currentstatus"]).ToString();
            }
            archive.AcceptChanges();
            return archive;
        }

        private void view_Exporttest()
        {
            using (WorkflowEntitySet context = new WorkflowEntitySet())
            {
                List<Alert> alerts = new List<Alert>();

                if (CurrentUser.Type == WorkflowEnums.UserType.Municipality)
                {
                    alerts = context.Alert.Where(a => a.archived == true && a.Municipality == context.Municipality.Where(m => m.id == CurrentUser.TypeId)).ToList<Alert>();
                }
                else if (CurrentUser.Type == WorkflowEnums.UserType.NetworkOperator)
                {
                    alerts = context.Alert.Where(a => a.archived == true && a.NetworkOperator == context.NetworkOperator.Where(n => n.id == CurrentUser.TypeId)).ToList<Alert>();
                }
            }
        }

        #endregion
    }
}
